(function($) {
	const STORAGE_PREFIX = "sys_storage_";

	/**
	 * 设置本地存储
	 * @param {string} key
	 * @param {string} value
	 * @return {boolean}
	 */
	function setStorage(key, value) {
		if (window.localStorage) {
			localStorage.setItem(STORAGE_PREFIX + key, JSON.stringify(value));
			return true;
		}
		return false;
	}

	$.define('setStorage', setStorage);

	/**
	 * 获取本地存储值
	 * @param {string} key
	 * @param {*} defaultValue
	 * @return {*}
	 */
	function getStorage(key, defaultValue = null) {
		if (window.localStorage) {
			try {
				let value = localStorage.getItem(STORAGE_PREFIX + key);
				if (value) {
					return JSON.parse(value);
				}
			} catch (e) {
			}
		}
		return defaultValue;
	}

	$.define('getStorage', getStorage);

	/**
	 * 移除本地存储值
	 * @param {String} key
	 */
	function removeStorage(key) {
		if (window.localStorage) {
			localStorage.removeItem(STORAGE_PREFIX + key);
			return true;
		}
		return false;
	}

	$.define('removeStorage', removeStorage);

	/**
	 * 清除本地存储
	 */
	function clearStorage() {
		if (window.localStorage) {
			for (let i = 0; i < localStorage.length; i++) {
				const key = localStorage.key(i);
				if (key.startsWith(STORAGE_PREFIX)) {
					localStorage.removeItem(key);
				}
			}
			return true;
		}
		return false;
	}

	$.define('clearStorage', clearStorage);

})($);
